package com.sf.proxy.custom.dynamic.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;


public class HuangNiuProxy implements MethodInterceptor {

    private Object object;

    public Object getProxyInstance(Class cla) {
        //代理的工具类
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(cla);
        //指定回调的 对象
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object o1 = methodProxy.invokeSuper(o, objects);
        after();
        return o1;
    }

    private void before(){
        System.out.println("买票前,先收钱");
    }
    private void after(){
        System.out.println("买票后,通知用户买好了");
    }
}
